/**
 * 虚拟数据脚本2
 */
dev.pick = function () {
    zhuangtai();
    dianliu();
    dianya();
    leijiliang();
    shunshiliuliang();
    ludian();
    press();
    chuQiPress();
    famen();
    leijishijian();
    YeWeii();
    yunxingcishu();
    othersss();
    coldEnPar();
    coldChwPar();
    coldCoolWaterPar();
    windCabInet();
}

function leijishijian () {
    var checkSql = 'select vir.dev_id as devId, vir.name as proName, vir.unit as unit  from iot_virtual_item vir where vir.name like \'%累积时间%\'';

    var lists = iot.makeAndGetCache('PROS_LIST_CACHE_SHIJIAN', null)

    if (lists == null) {
        // 如果没有缓存，第一次，从数据库加载数据
        lists = iot.db().queryList(checkSql);
        if (lists.size() > 0) {
            iot.makeAndGetCache('PROS_LIST_CACHE_SHIJIAN', lists)
        }
    }


    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i)
        var getOldValueSql = 'SELECT dvalue, uptime from iot_dev_cur_data_win cur where cur.dev_id = ' + item.devId + ' and data_type = "' + item.proName + '"';
        var oldValueMap = iot.db().queryOne(getOldValueSql);
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 5)
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH')
        var unit = item.unit?item.unit:''

        if (oldValueMap.dvalue == null && oldValueMap.uptime == null) {
            // 插入实时表 （插入 秒粒度的时间）
            var replaceCurSql = 'REPLACE into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) value (' + item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
            iot.db().updateAsync(replaceCurSql);
            continue
        }

        if (oldValueMap.dvalue != null) {
            dvalue = parseInt(oldValueMap.dvalue) + 1
        }

        if (oldValueMap.uptime != null) {
            var gapMinutes = parseInt(new Date() - new Date(oldValueMap.uptime)) / 1000 /60
            // 20秒插入一条
            if (gapMinutes > 5) {
                // 插入实时表
                var replaceCurSql = 'REPLACE into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) value (' + item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
                iot.db().updateAsync(replaceCurSql);
            }
        }
    }
}
function yunxingcishu () {
    var checkSql = 'select vir.dev_id as devId, vir.name as proName, vir.unit as unit  from iot_virtual_item vir where vir.name like \'%运行次数%\'';

    var lists = iot.makeAndGetCache('PROS_LIST_CACHE_CISHU', null)

    if (lists == null) {
        // 如果没有缓存，第一次，从数据库加载数据
        lists = iot.db().queryList(checkSql);
        if (lists.size() > 0) {
            iot.makeAndGetCache('PROS_LIST_CACHE_CISHU', lists)
        }
    }


    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i)
        var getOldValueSql = 'SELECT dvalue, uptime from iot_dev_cur_data_win cur where cur.dev_id = ' + item.devId + ' and data_type = "' + item.proName + '"';
        var oldValueMap = iot.db().queryOne(getOldValueSql);
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 5)
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH')
        var unit = item.unit?item.unit:'次'

        if (oldValueMap.dvalue == null && oldValueMap.uptime == null) {
            // 插入实时表 （插入 秒粒度的时间）
            var replaceCurSql = 'REPLACE into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) value (' + item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
            iot.db().updateAsync(replaceCurSql);
            continue
        }

        if (oldValueMap.dvalue != null) {
            dvalue = parseInt(oldValueMap.dvalue) + 1
        }

        if (oldValueMap.uptime != null) {
            var gapMinutes = parseInt(new Date() - new Date(oldValueMap.uptime)) / 1000 /60
            // 20秒插入一条
            if (gapMinutes > 5) {
                // 插入实时表
                var replaceCurSql = 'REPLACE into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) value (' + item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
                iot.db().updateAsync(replaceCurSql);
            }
        }
    }
}

function zhuangtai () {
    var checkSql = 'select vir.dev_id as devId, vir.name as proName, vir.unit as unit  from iot_virtual_item vir where vir.name like \'%运行状态%\'';

    var checkResult = iot.makeAndGetCache('PROS_LIST_CACHE_ZHUANGTAI', null)
    if (checkResult == null) {
        // 如果没有缓存，第一次，从数据库加载数据
        checkResult = iot.db().queryList(checkSql);
        if (checkResult.size() > 0) {
            iot.makeAndGetCache('PROS_LIST_CACHE_ZHUANGTAI', checkResult)
        }
    }

    for (var i = 0; i < checkResult.size(); i++) {
        var item = checkResult.get(i)
        var getOldValueSql = 'SELECT dvalue, uptime from iot_dev_cur_data_win cur where cur.dev_id = ' + item['devId'] + ' and data_type = "' + item['proName'] + '"';
        var oldValueMap = iot.db().queryOne(getOldValueSql);
        var unit = '';

        var targetNum1 = parseInt(Math.random() * 33)%2;
        var targetNum2 = parseInt(Math.random() * 33)%2;
        var targetNum = targetNum1 + targetNum2;


        if (oldValueMap.uptime != null) {
            var gapMinutes = parseInt(new Date() - new Date(oldValueMap.uptime)) / 1000 / 60
            // 5小时 插入一条
            if (gapMinutes < 300) {
                targetNum = oldValueMap['dvalue']
            }
            var replaceCurSql = 'REPLACE into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) value (' + item['devId'] + ',"' + item['proName'] + '",' + targetNum + ',"' + unit + '",' + 'now())';
            iot.db().updateAsync(replaceCurSql);
        }

    }
}

function dianliu () {
    var checkSql = 'select vir.dev_id as devId, vir.name as proName, vir.unit as unit  from iot_virtual_item vir where vir.property_type = 100 and vir.name like \'%电流%\'';

    var lists = iot.makeAndGetCache('PROS_LIST_CACHE_DIANLIU', null)

    if (lists == null) {
        // 如果没有缓存，第一次，从数据库加载数据
        lists = iot.db().queryList(checkSql);
        if (lists.size() > 0) {
            iot.makeAndGetCache('PROS_LIST_CACHE_DIANLIU', lists)
        }
    }
    var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';
    var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) values ';
    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i);
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 128 + 100);
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH');
        var unit = item.unit?item.unit:'';
        replaceSql = replaceSql + '('+ item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
        insertIntoWinSql = insertIntoWinSql + '(' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+ item.proName + '",' + dvalue + ')';
        if (i < lists.size() - 1) {
            replaceSql = replaceSql + ', ';
            insertIntoWinSql = insertIntoWinSql + ', ';
        }
    }
    iot.db().updateAsync(replaceSql);
    iot.db().updateAsync(insertIntoWinSql);

}

function dianya () {
    var checkSql = 'select vir.dev_id as devId, vir.name as proName, vir.unit as unit  from iot_virtual_item vir where vir.property_type = 100 and vir.name like \'%电压%\'';

    var lists = iot.makeAndGetCache('PROS_LIST_CACHE_DIANYA', null)

    if (lists == null) {
        // 如果没有缓存，第一次，从数据库加载数据
        lists = iot.db().queryList(checkSql);
        if (lists.size() > 0) {
            iot.makeAndGetCache('PROS_LIST_CACHE_DIANYA', lists)
        }
    }
    var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';
    var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) values ';
    var insertIntoWinMin = 'REPLACE INTO iot_dev_data_win_minute (dev_id,order_num,the_time,data_type,dvalue) values ';
    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i);
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 20 + 200);
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH');
        var unit = item.unit?item.unit:'';
        replaceSql = replaceSql + '('+ item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
        insertIntoWinSql = insertIntoWinSql + '(' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+ item.proName + '",' + dvalue + ')';
        insertIntoWinMin = insertIntoWinMin + '(' + item.devId + ',1,date_format(now(), "%Y-%m-%d %H:%i"),"'+ item.proName + '",' + dvalue + ')';
        if (i < lists.size() - 1) {
            replaceSql = replaceSql + ', ';
            insertIntoWinSql = insertIntoWinSql + ', ';
            insertIntoWinMin = insertIntoWinMin + ', ';
        }
    }
    iot.db().updateAsync(replaceSql);
    iot.db().updateAsync(insertIntoWinSql);
    iot.db().updateAsync(insertIntoWinMin);
}

function leijiliang () {

    var checkSql = 'select vir.dev_id as devId, vir.name as proName, vir.unit as unit  from iot_virtual_item vir where vir.property_type = 100 and vir.name like concat(\'%\',\'累积流量\',\'%\')';

    var lists = iot.makeAndGetCache('PROS_LIST_CACHE_LEIJILIANG', null)

    if (lists == null) {
        // 如果没有缓存，第一次，从数据库加载数据
        lists = iot.db().queryList(checkSql);
        if (lists.size() > 0) {
            iot.makeAndGetCache('PROS_LIST_CACHE_LEIJILIANG', lists)
        }
    }


    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i)
        var getOldValueSql = 'SELECT dvalue, uptime from iot_dev_cur_data_win cur where cur.dev_id = ' + item.devId + ' and data_type = "' + item.proName + '"';
        var oldValueMap = iot.db().queryOne(getOldValueSql);
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 5)
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH')
        var unit = item.unit?item.unit:''

        if (oldValueMap.dvalue == null && oldValueMap.uptime == null) {
            // 插入实时表 （插入 秒粒度的时间）
            var replaceCurSql = 'REPLACE into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) value (' + item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
            iot.db().updateAsync(replaceCurSql);
            // 插入win表 插入 小时粒度的时间
            var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) value (' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+',"' + item.proName + '",' + dvalue + ')';
            iot.db().updateAsync(insertIntoWinSql);
            // 插入win表 插入 分钟粒度的时间
            var insertIntoWinMinSql = 'REPLACE INTO iot_dev_data_win_minute (dev_id,order_num,the_time,data_type,dvalue) value (' + item.devId + ',1,date_format(now(), "%Y-%m-%d %H:%i"),"' + item.proName + '",' + dvalue + ')';
            iot.db().updateAsync(insertIntoWinMinSql);
            continue
        }

        if (oldValueMap.dvalue != null) {
            dvalue = parseInt(oldValueMap.dvalue) + parseInt(dvalue)
        }

        var ifReplace = false;
        if (oldValueMap.uptime != null) {
            var gapMinutes = parseInt(new Date() - new Date(oldValueMap.uptime)) / 1000
            // 20秒插入一条
            if (gapMinutes > 3) {
                ifReplace = true
            }
        }

        if (ifReplace) {
            // 插入实时表
            var replaceCurSql = 'REPLACE into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) value (' + item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())';
            iot.db().updateAsync(replaceCurSql);
            // 插入win表
            var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) value (' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+ item.proName + '",' + dvalue + ')';
            iot.db().updateAsync(insertIntoWinSql);
            // 插入win表
            var insertIntoWinMinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) value (' + item.devId + ',1,date_format(now(), "%Y-%m-%d %H:%i"),"'+ item.proName + '",' + dvalue + ')';
            iot.db().updateAsync(insertIntoWinMinSql);
        }
    }
}

function shunshiliuliang () {

    var checkSql = 'select vir.dev_id as devId, vir.name as proName, vir.unit as unit  from iot_virtual_item vir where vir.property_type = 100 and vir.name like concat(\'%\',\'瞬时流量\',\'%\')';

    var lists = iot.makeAndGetCache('PROS_LIST_CACHE_LEIJILIANGLI', null)

    if (lists == null) {
        // 如果没有缓存，第一次，从数据库加载数据
        lists = iot.db().queryList(checkSql);
        if (lists.size() > 0) {
            iot.makeAndGetCache('PROS_LIST_CACHE_LEIJILIANGLI', lists)
        }
    }

    var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';
    var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) values ';
    var insertIntoWinMin = 'REPLACE INTO iot_dev_data_win_minute (dev_id,order_num,the_time,data_type,dvalue) values ';
    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i)
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 245 + 88)
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH')
        var unit = item.unit?item.unit:''
        replaceSql = replaceSql + '('+ item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())'
        insertIntoWinSql = insertIntoWinSql + '(' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+ item.proName + '",' + dvalue + ')';
        insertIntoWinMin = insertIntoWinMin + '(' + item.devId + ',1,date_format(now(), "%Y-%m-%d %H:%i"),"'+ item.proName + '",' + dvalue + ')';
        if (i < lists.size() - 1) {
            replaceSql = replaceSql + ', ';
            insertIntoWinSql = insertIntoWinSql + ', ';
            insertIntoWinMin = insertIntoWinMin + ', ';
        }
    }
    iot.db().updateAsync(replaceSql);
    iot.db().updateAsync(insertIntoWinSql);
    iot.db().updateAsync(insertIntoWinMin);
}

function ludian () {

    var checkSql = 'select vir.dev_id as devId, vir.name as proName, vir.unit as unit  from iot_virtual_item vir where vir.property_type = 100 and vir.name like concat(\'%\',\'露点\',\'%\')';

    var lists = iot.makeAndGetCache('PROS_LIST_CACHE_LUDIANN', null)

    if (lists == null) {
        // 如果没有缓存，第一次，从数据库加载数据
        lists = iot.db().queryList(checkSql);
        if (lists.size() > 0) {
            iot.makeAndGetCache('PROS_LIST_CACHE_LUDIANN', lists)
        }
    }

    var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';
    var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) values ';
    var insertIntoWinMin = 'REPLACE INTO iot_dev_data_win_minute (dev_id,order_num,the_time,data_type,dvalue) values ';
    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i)
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 17 - 17)
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH')
        var unit = item.unit?item.unit:''
        replaceSql = replaceSql + '('+ item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())'
        insertIntoWinSql = insertIntoWinSql + '(' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+ item.proName + '",' + dvalue + ')';
        insertIntoWinMin = insertIntoWinMin + '(' + item.devId + ',1,date_format(now(), "%Y-%m-%d %H:%i"),"'+ item.proName + '",' + dvalue + ')';
        if (i < lists.size() - 1) {
            replaceSql = replaceSql + ', ';
            insertIntoWinSql = insertIntoWinSql + ', ';
            insertIntoWinMin = insertIntoWinMin + ', ';
        }
    }
    iot.db().updateAsync(replaceSql);
    iot.db().updateAsync(insertIntoWinSql);
    iot.db().updateAsync(insertIntoWinMin);
}

function press () {

    var checkSql = 'select vir.dev_id as devId, vir.name as proName, vir.unit as unit  from iot_virtual_item vir where vir.property_type = 100 and vir.name like concat(\'%\',\'当前压力\',\'%\')';

    var lists = iot.makeAndGetCache('PROS_LIST_PRESS', null)

    if (lists == null) {
        // 如果没有缓存，第一次，从数据库加载数据
        lists = iot.db().queryList(checkSql);
        if (lists.size() > 0) {
            iot.makeAndGetCache('PROS_LIST_PRESS', lists)
        }
    }

    var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';
    var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) values ';
    var insertIntoWinMin = 'REPLACE INTO iot_dev_data_win_minute (dev_id,order_num,the_time,data_type,dvalue) values ';
    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i)
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 100 + 100)
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH')
        var unit = item.unit?item.unit:''
        replaceSql = replaceSql + '('+ item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())'
        insertIntoWinSql = insertIntoWinSql + '(' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+ item.proName + '",' + dvalue + ')';
        insertIntoWinMin = insertIntoWinMin + '(' + item.devId + ',1,date_format(now(), "%Y-%m-%d %H:%i"),"'+ item.proName + '",' + dvalue + ')';
        if (i < lists.size() - 1) {
            replaceSql = replaceSql + ', ';
            insertIntoWinSql = insertIntoWinSql + ', ';
            insertIntoWinMin = insertIntoWinMin + ', ';
        }
    }
    iot.db().updateAsync(replaceSql);
    iot.db().updateAsync(insertIntoWinSql);
    iot.db().updateAsync(insertIntoWinMin);
}

function chuQiPress () {

    var checkSql = 'select vir.dev_id as devId, vir.name as proName, vir.unit as unit  from iot_virtual_item vir where vir.property_type = 100 and vir.name like concat(\'%\',\'出气压力\',\'%\')';

    var lists = iot.makeAndGetCache('PROS_LIST_PRESS22', null)

    if (lists == null) {
        // 如果没有缓存，第一次，从数据库加载数据
        lists = iot.db().queryList(checkSql);
        if (lists.size() > 0) {
            iot.makeAndGetCache('PROS_LIST_PRESS22', lists)
        }
    }

    var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';
    var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) values ';
    var insertIntoWinMin = 'REPLACE INTO iot_dev_data_win_minute (dev_id,order_num,the_time,data_type,dvalue) values ';
    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i)
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 3 + 15)
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH')
        var unit = item.unit?item.unit:'Mpa'
        replaceSql = replaceSql + '('+ item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())'
        insertIntoWinSql = insertIntoWinSql + '(' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+ item.proName + '",' + dvalue + ')';
        insertIntoWinMin = insertIntoWinMin + '(' + item.devId + ',1,date_format(now(), "%Y-%m-%d %H:%i"),"'+ item.proName + '",' + dvalue + ')';
        if (i < lists.size() - 1) {
            replaceSql = replaceSql + ', ';
            insertIntoWinSql = insertIntoWinSql + ', ';
            insertIntoWinMin = insertIntoWinMin + ', ';
        }
    }
    iot.db().updateAsync(replaceSql);
    iot.db().updateAsync(insertIntoWinSql);
    iot.db().updateAsync(insertIntoWinMin);
}

function YeWeii () {

    var checkSql = 'select vir.dev_id as devId, vir.name as proName, vir.unit as unit  from iot_virtual_item vir where vir.property_type = 100 and vir.name like concat(\'%\',\'液位\',\'%\')';

    var lists = iot.makeAndGetCache('PROS_LIST_YEWEI2', null)

    if (lists == null) {
        // 如果没有缓存，第一次，从数据库加载数据
        lists = iot.db().queryList(checkSql);
        if (lists.size() > 0) {
            iot.makeAndGetCache('PROS_LIST_YEWEI2', lists)
        }
    }

    var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';
    var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) values ';
    var insertIntoWinMin = 'REPLACE INTO iot_dev_data_win_minute (dev_id,order_num,the_time,data_type,dvalue) values ';
    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i)
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 3 + 3)
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH')
        var unit = item.unit?item.unit:'Mpa'
        replaceSql = replaceSql + '('+ item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())'
        insertIntoWinSql = insertIntoWinSql + '(' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+ item.proName + '",' + dvalue + ')';
        insertIntoWinMin = insertIntoWinMin + '(' + item.devId + ',1,date_format(now(), "%Y-%m-%d %H:%i"),"'+ item.proName + '",' + dvalue + ')';
        if (i < lists.size() - 1) {
            replaceSql = replaceSql + ', ';
            insertIntoWinSql = insertIntoWinSql + ', ';
            insertIntoWinMin = insertIntoWinMin + ', ';
        }
    }
    iot.db().updateAsync(replaceSql);
    iot.db().updateAsync(insertIntoWinSql);
    iot.db().updateAsync(insertIntoWinMin);
}


function famen () {

    var checkSql = 'select vir.dev_id as devId, vir.name as proName, vir.unit as unit  from iot_virtual_item vir where vir.property_type = 100 and vir.name like concat(\'%\',\'当前压力\',\'%\')';

    var lists = iot.makeAndGetCache('PROS_LIST_FAMEN', null)

    if (lists == null) {
        // 如果没有缓存，第一次，从数据库加载数据
        lists = iot.db().queryList(checkSql);
        if (lists.size() > 0) {
            iot.makeAndGetCache('PROS_LIST_FAMEN', lists)
        }
    }

    var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';
    var insertIntoWinSql = 'REPLACE INTO iot_dev_data_win (dev_id,order_num,the_time,data_type,dvalue) values ';
    var insertIntoWinMin = 'REPLACE INTO iot_dev_data_win_minute (dev_id,order_num,the_time,data_type,dvalue) values ';

    for (var i = 0; i < lists.size(); i++) {
        var item = lists.get(i)
        var dvalue = iot.getNumberFormat('0').format(iot.rand() * 30 +70)
        var curTime = dtime.getCurrentDateTime().toString('yyyy-MM-dd HH')
        var unit = item.unit?item.unit:''
        replaceSql = replaceSql + '('+ item.devId + ',"' + item.proName + '",' + dvalue + ',"' + unit + '",' + 'now())'
        insertIntoWinSql = insertIntoWinSql + '(' + item.devId + ',1,STR_TO_DATE("'+curTime+'","%Y-%m-%d %H"), "'+ item.proName + '",' + dvalue + ')';
        insertIntoWinMin = insertIntoWinMin + '(' + item.devId + ',1,date_format(now(), "%Y-%m-%d %H:%i"),"'+ item.proName + '",' + dvalue + ')';

        if (i < lists.size() - 1) {
            replaceSql = replaceSql + ', ';
            insertIntoWinSql = insertIntoWinSql + ', ';
            insertIntoWinMin = insertIntoWinMin + ', ';
        }
    }
    iot.db().updateAsync(replaceSql);
    iot.db().updateAsync(insertIntoWinSql);
    iot.db().updateAsync(insertIntoWinMin);
}

function othersss () {
    var devId = 10608;
    var pros = ['CO2浓度','PM2.5','平均温度','平均湿度','CO2浓度','总管累积流量','供气压力','供气露点','供气流量1','供气流量2','供水压力','供水温度1','供水温度2','供水液位'];
    var units = ['kg/m3','μg/m3','℃','℃','kg/m','m³','Mpa','℃td','m³/min','m³/min','Mpa','℃','℃','m'];

    var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';

    for (var i = 0; i < pros.length; i++) {
        var dvalue = 0;
        if (pros[i].indexOf('温度') > -1) {
            dvalue = iot.getNumberFormat('0').format(iot.rand() * 20 +10);
        } else if (pros[i].indexOf('浓度') > -1) {
            dvalue = iot.getNumberFormat('0').format(iot.rand() * 130 +100);
        } else if (pros[i].indexOf('总管累积流量') > -1) {
            dvalue = iot.getNumberFormat('0').format(iot.rand() * 11230 +103430);
        } else if (pros[i].indexOf('供气露点') > -1) {
            dvalue = iot.getNumberFormat('0').format(iot.rand() * 17 -17);
        }else if (pros[i].indexOf('供水液位') > -1) {
            dvalue = iot.getNumberFormat('0').format(iot.rand() * 3 +3);
        }else {
            dvalue = iot.getNumberFormat('0').format(iot.rand() * 230 +100);
        }
        var unit = '';
        replaceSql = replaceSql + '('+ devId + ',"' + pros[i] + '",' + dvalue + ',"' + units[i] + '",' + 'now())'
        if (i < pros.length - 1) {
            replaceSql = replaceSql + ', ';
        }
    }

    iot.db().updateAsync(replaceSql);

}
function coldEnPar () {
    var devId = [10616,10659,10663];
    var pros = ['CO2浓度','PM25','平均温度','平均湿度','CO2浓度','PM25'];
    var units = ['kg/m3','μg/m3','℃','℃','kg/m','μg/m3'];

    for (var dId = 0; dId < devId.length; dId++) {
        var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';

        for (var i = 0; i < pros.length; i++) {
            var dvalue = 0;
            if (pros[i].indexOf('温度') > -1) {
                dvalue = iot.getNumberFormat('0').format(iot.rand() * 20 + 10);
            } else if (pros[i].indexOf('湿度') > -1) {
                dvalue = iot.getNumberFormat('0').format(iot.rand() * 20 + 10);
            } else if (pros[i].indexOf('浓度') > -1) {
                dvalue = iot.getNumberFormat('0').format(iot.rand() * 130 + 100);
            } else {
                dvalue = iot.getNumberFormat('0').format(iot.rand() * 230 + 100);
            }
            var unit = '';
            replaceSql = replaceSql + '(' + devId[dId] + ',"' + pros[i] + '",' + dvalue + ',"' + units[i] + '",' + 'now())'
            if (i < pros.length - 1) {
                replaceSql = replaceSql + ', ';
            }
        }

        iot.db().updateAsync(replaceSql);
    }

}
function coldChwPar () {
    var devId = [10617,10660,10664];
    var pros = ['供气压力', '供气露点', '瞬时流量', '累积流量'];
    var units = ['MPa', '℃td', 'm³/h', 'm³'];

    for (var dId = 0; dId < devId.length; dId++) {
        var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';

        for (var i = 0; i < pros.length; i++) {
            var dvalue = 0;
            if (pros[i].indexOf('供气压力') > -1) {
                dvalue = iot.getNumberFormat('0').format(iot.rand() * 20 + 10);
            } else {
                dvalue = iot.getNumberFormat('0').format(iot.rand() * 230 + 100);
            }
            replaceSql = replaceSql + '(' + devId[dId] + ',"' + pros[i] + '",' + dvalue + ',"' + units[i] + '",' + 'now())'
            if (i < pros.length - 1) {
                replaceSql = replaceSql + ', ';
            }
        }

        iot.db().updateAsync(replaceSql);
    }
}
function coldCoolWaterPar () {
    var devId = [10618, 10661,10665];
    var pros = ['供水压力','回水压力','回水温度', '瞬时流量', '累积流量', '水池液位'];
    var units = ['MPa','MPa','℃', 'm³/h', 'm³', 'm'];

    for (var dId = 0; dId < devId.length; dId++) {
        var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';

        for (var i = 0; i < pros.length; i++) {
            var dvalue = 0;
            if (pros[i].indexOf('供气压力') > -1) {
                dvalue = iot.getNumberFormat('0').format(iot.rand() * 20 + 10);
            } else if (pros[i].indexOf('回水压力') > -1) {
                dvalue = iot.getNumberFormat('0').format(iot.rand() * 20 + 10);
            } else if (pros[i].indexOf('回水温度') > -1) {
                dvalue = iot.getNumberFormat('0').format(iot.rand() * 20 + 10);
            } else {
                dvalue = iot.getNumberFormat('0').format(iot.rand() * 20 + 1);
            }
            replaceSql = replaceSql + '(' + devId[dId] + ',"' + pros[i] + '",' + dvalue + ',"' + units[i] + '",' + 'now())'
            if (i < pros.length - 1) {
                replaceSql = replaceSql + ', ';
            }
        }

        iot.db().updateAsync(replaceSql);
    }
}
function windCabInet () {
    var devId = [10666, 10667, 10668, 10669, 10670, 10671, 10672, 10673, 10674, 10675];
    var pros = ['累计冷量','累计流量','供冷压力', '供冷温度', '阀门状态'];
    var units = ['KGJ','m³/h','MPa', '℃', '%'];
    for (var dId = 0; dId < devId.length; dId++) {
        var replaceSql = 'replace into iot_dev_cur_data_win (dev_id,data_type,dvalue,unit,uptime) values ';

        for (var i = 0; i < pros.length; i++) {
            var dvalue = 0;
            if (pros[i].indexOf('累计') > -1) {
                dvalue = iot.getNumberFormat('0').format(iot.rand() * 200 + 100);
            } else {
                dvalue = iot.getNumberFormat('0').format(iot.rand() * 20 + 1);
            }
            replaceSql = replaceSql + '(' + devId[dId] + ',"' + pros[i] + '",' + dvalue + ',"' + units[i] + '",' + 'now())'
            if (i < pros.length - 1) {
                replaceSql = replaceSql + ', ';
            }
        }

        iot.db().updateAsync(replaceSql);
    }
}
